<?php
/*
* ----------------------------------------------------------------------------------------------------
* Options Generator
* @PACKAGE BY TONYTHEMES
* ----------------------------------------------------------------------------------------------------
*/
if(!class_exists('theme_options_generator'))
{

	class theme_options_generator 
	{
		var $name;
		var $options;
		var $saved_options;

		function theme_options_generator($name, $options) 
		{
		
			$this->name = $name;
			$this->options = $options;
			
			$this->save_options();
			$this->render();

		}

		/* SAVE OPTIONS */
		function save_options() 
		{
			$options = get_option('theme_' .THEME_SLUG . '_' . $this->name);
			
			if (isset($_POST['save_theme_options'])) {
				
				foreach($this->options as $value) {
					if (isset($value['id']) && ! empty($value['id'])) {
						if (isset($_POST[$value['id']])) {
							if ($value['type'] == 'multidropdown') {
								if(empty($_POST[$value['id']])){
									$options[$value['id']] = array();
								}else{
									$options[$value['id']] = array_unique(explode(',', $_POST[$value['id']]));
								}
							} else {
								$options[$value['id']] = $_POST[$value['id']];
							}
						} else {
							$options[$value['id']] = false;
						}
					}
				}

				if ($options != $this->options) {
					update_option('theme_' .THEME_SLUG . '_' . $this->name, $options);
				}
				echo '<div id="message" class="updated fade"><p><strong>Updated Successfully</strong></p></div>';					
			}
			
			$this->saved_options = $options;

		}


		/*render*/
		function render() {
			echo '<div class="wrap theme_center">';
			echo '<form method="post" action="">';
			
			foreach($this->options as $option) {
				if (method_exists($this, $option['type'])) {
					$this->$option['type']($option);
				}
			
			}

			echo '<p class="submit"><input type="submit" name="save_theme_options" class="button-primary" value="Save Changes" /></p>';
			echo '</form>';
			echo '</div>';
		}

		
		
		
		
		/*prints the options page title*/
		function title($value) {
			echo '<div class="icon32" id="icon-options-general"><br/></div>';
			echo '<h2>' . $value['name'] . '<span>version ' .THEME_VERSION. '</span></h2>';
			if (isset($value['desc'])) {
				echo '<p>' . $value['desc'] . '</p>';
			}
		}

		/*prints the options page sub title*/
		function sub_title($value) {
			echo '<label>'.$value["name"]; 
			if($value['desc'] <> ''){
			echo '(<span>'.$value['desc'].'</span>)';
			} 
			echo '</label>';
		}

	 /* Begin a box */
	 function head($value)
	 {
	 echo '<fieldset>';
	 }

	 /**
	 * end the group section
	 */
		function foot($value) {
			echo '</fieldset>';
		}


	  /* displays a upload*/
		function upload($value) {
			$size = isset($value['size']) ? $value['size'] : '8';
			$button = isset($value['button']) ? $value['button'] : 'Upload Image';

			echo '<section><label for="'.$value['id'].'">' . $value['name'] . '</label><div>';
			
			if(isset($value['title'])){
						echo '<span for="' . $value['id'] . '">'. $value['title'] .'</span><br />';
			}

			echo '<input name="' . $value['id'] . '" id="' . $value['id'] . '" type="text" size="' . $size . '" value="';
			if (isset($this->saved_options[$value['id']])) {
				echo stripslashes($this->saved_options[$value['id']]);
			} else {
				echo $value['std'];
			}
			echo '" />';

			echo '<input id="' . $value['id'] . '_button" name="' . $value['id'] . '_button" type="button" value="'.$button.'" class="button-secondary" />';

			if(isset($value['desc'])){
				echo '<p class="description">' . $value['desc'] . '</p>';
			}
			echo '</div></section>';
		}


	 /**
	 * displays a color input
	 */
		function color($value) {
			$size = isset($value['size']) ? $value['size'] : '10';

			if (isset($this->saved_options[$value['id']])) {
				$val = stripslashes($this->saved_options[$value['id']]);
			} else {
				$val = $value['std'];
			}
			
			echo '<section><label for="'.$value['id'].'">' . $value['name'] . '</label><div>';
			echo '<input name="' . $value['id'] . '" id="' . $value['id'] . '" type="text" size="' . $size . '" class="color-text-'.$value['id'].'" value="'. $val .'" />';
			echo '<span id="colorSelector_'. $value['id'] .'" class="theme_center_color_picker colorSelector"><div></div></span>';
			
			if(isset($value['desc'])){
				echo '<p class="description">' . $value['desc'] . '</p>';
			}


			echo '</div></section>';

			echo '<script type="text/javascript">'."\n";
			echo '//<![CDATA['."\n";
			echo 'jQuery(document).ready(function(){'."\n";
			echo 'jQuery("#colorSelector_'. $value['id'] .' div").css("backgroundColor", "#'. $val .'");'."\n";    
			echo '    jQuery("span#colorSelector_'. $value['id'] .'").ColorPicker({'."\n";
			echo '       color: "#'. $val .'",'."\n";
			echo '       onShow: function (colpkr) {'."\n";
			echo '			  jQuery(colpkr).fadeIn(500);'."\n";
			echo '			  return false;'."\n";
			echo '		   },'."\n";
			echo '		   onHide: function (colpkr) {'."\n";
			echo '				jQuery(colpkr).fadeOut(500);'."\n";
			echo '				return false;'."\n";
			echo '			},'."\n";
			echo '			onChange: function (hsb, hex, rgb) {'."\n";
			echo '				jQuery("#colorSelector_'. $value['id'] .' div").css("backgroundColor", "#" + hex);'."\n";
			echo '				jQuery("#'.$value['id'].'").val(hex);'."\n";
			echo '			}'."\n";
			echo '		});'."\n";
			echo '	});'."\n";
			echo '//]]>'."\n";
			echo '</script>'."\n";
		}


		/**
		 * displays a text input
		*/
		function text($value) {
			$size = isset($value['size']) ? $value['size'] : '10';
			
			echo '<section><label for="'.$value['id'].'">' . $value['name'] . '</label><div>';
			echo '<input name="' . $value['id'] . '" id="' . $value['id'] . '" type="text" size="' . $size . '" value="';
			if (isset($this->saved_options[$value['id']])) {
				echo stripslashes($this->saved_options[$value['id']]);
			} else {
				echo $value['std'];
			}
			echo '"';
			if(isset($value['placehoder']))
			{
			echo ' placeholder="'.$value['placehoder'].'"';
			}
			echo ' />';
			if(isset($value['unit'])){ echo '<em class="unit">' . $value['unit'] . '</em>'; }
			if(isset($value['desc'])){
				echo '<p class="description">' . $value['desc'] . '</p>';
			}
			echo '</div></section>';
		}

		/* Display custom sidebar */
		function add_custom_sidebar($value) {
			$size = isset($value['size']) ? $value['size'] : '10';
			
			echo '<section><label for="'.$value['id'].'">' . $value['name'] . '</label><div>';
    		echo '  <input type="text" width='.$value['size'].' id="input_add_sidebar" name="add_sidebar" /><input id="btn_add" value="Add Sidebar" type="button" name="btn_add" style="cursor:pointer"/>
					<div id="control_add_sidebar">';
			if (isset($this->saved_options[$value['id']])) {
				$current = $this->saved_options[$value['id']];
				$Arrs = explode(",",$current);
				foreach($Arrs as $arr)
				{
				if($arr <> ''):
				echo '<p style="margin-top: 10px;" id="'.$arr.'"><span style="font-size: 14px;" id="'.$arr.'" class="sidebar_value">'.str_replace("_"," ",$arr).'</span><span style="margin-left: 10px; color: red; cursor: pointer;" id="del_'.$arr.'">  DELETE</span></p>';
			echo ' 	 	<script>';
			echo '		jQuery("#del_'.$arr.'").click(function(){'."\n";
			echo '			var test = confirm("Do you want delete this Sidebar?");'."\n";
			echo '			if(test == true)'."\n";
			echo '			{'."\n";
			echo '			jQuery(this).parent().remove();'."\n";
			echo '			updatevalue();'."\n";
			echo '			}'."\n";
			echo '		});'."\n";		
			echo '		</script>'."\n";		
				 endif;
				}
			
			} else {
				
			}		
					
			echo '	</div>
					<input type="hidden" id="'.$value['id'].'" name="'.$value['id'].'" value="';
			if (isset($this->saved_options[$value['id']])) {
				echo stripslashes($this->saved_options[$value['id']]);
			} else {
				
			}
			echo '"';
			echo ' />';
			echo '</div></section>';
			
			echo '<script>'."\n";
			echo '	function isset(element) {'."\n";
			echo '		return element.length > 0;'."\n";
			echo '	}'."\n";
			echo '	function updatevalue()'."\n";
			echo '	{'."\n";
			echo '	textresult = new Array();'."\n";
			echo '	jQuery("span.sidebar_value").each(function(i){'."\n";
			echo '	textresult[i] = jQuery(this).attr("ID");'."\n";
				
			echo '	});'."\n";
			echo '	result = textresult.join(",");'."\n";
			echo '	jQuery("#add_custom_sidebar").val(result);'."\n";
			echo '	};'."\n";
			echo '	jQuery("#btn_add").click(function(){'."\n";
			echo '		value = jQuery("#input_add_sidebar").val();'."\n";
					
			echo '		id_sidebar= jQuery("#input_add_sidebar").val().replace(" ", "_").replace(" ", "_").replace(" ", "_").replace(" ", "_");'."\n";
			echo '		id_sidebar_del= "del_" + jQuery("#input_add_sidebar").val().replace(" ", "_").replace(" ", "_").replace(" ", "_").replace(" ", "_");'."\n";
			echo '		if(value.length > 0 && !isset(jQuery("#"+id_sidebar)))'."\n";
			echo '		{'."\n";
			echo '			jQuery("#control_add_sidebar").append(\'<p id="\'+id_sidebar+\'" style="margin-top:10px;"><span class="sidebar_value" id="\'+id_sidebar+\'" style="font-size:14px">\'+value+\'</span><span id="\'+id_sidebar_del+\'" style="margin-left:10px;color:red;cursor:pointer">  DELETE</span></p>\');'."\n";
			echo '			updatevalue();'."\n";
						
			echo '		}else{'."\n";
			echo '			alert("The sidebar name must not exist and not null");'."\n";
			echo '		}'."\n";
					
			echo '		jQuery("#"+id_sidebar_del).bind("click",function(){'."\n";
			echo '			var test = confirm("Do you want delete this Sidebar?");'."\n";
			echo '			if(test == true)'."\n";
			echo '			{'."\n";
			echo '			jQuery(this).parent().remove();'."\n";
			echo '			updatevalue();'."\n";
			echo '			}'."\n";
			echo '		});'."\n";
			echo '	});'."\n";
				
			echo '</script>'."\n";
		}
	
		/* displays a textarea*/
		function textarea($value) {
			$rows = isset($value['rows']) ? $value['rows'] : '10';
			
			echo '<section><label for="'.$value['id'].'">' . $value['name'] . '</label><div>';
			echo '<textarea id="'.$value['id'].'" rows="' . $rows . '" name="' . $value['id'] . '" type="' . $value['type'] . '" class="code">';
			if (isset($this->saved_options[$value['id']])) {
				echo stripslashes($this->saved_options[$value['id']]);
			} else {
				echo $value['std'];
			}
			echo '</textarea>';
			if(isset($value['desc'])){
				echo '<p class="description">' . $value['desc'] . '</p>';
			}
			echo '</div></section>';
		}


		/**
	 * displays a select
	 */
		function select($value) {
			if (isset($value['target'])) {
				if (isset($value['options'])) {
					$value['options'] = $value['options'] + $this->get_select_target_options($value['target']);
				} else {
					$value['options'] = $this->get_select_target_options($value['target']);
				}
			}
			echo '<section><label for="'.$value['id'].'">' . $value['name'] . '</label><div>';
			echo '<select';
			if(isset($value['prompt'])){
			echo ' data-placeholder="'.$value['prompt'].'"';
			};
			echo ' name="' . $value['id'] . '"  class="chzn-select-deselect-'.$value['id'].'" style="width:350px;" tabindex="1">';
			
			foreach($value['options'] as $key => $option) {
				echo "<option value='" . $key . "'";
				if (isset($this->saved_options[$value['id']])) {
					if (stripslashes($this->saved_options[$value['id']]) == $key) {
						echo ' selected="selected"';
					}
				} else if ($key == $value['std']) {
					echo ' selected="selected"';
				}
				
				echo '>' . $option . '</option>';
			}
			
			echo '</select>';

			if(isset($value['desc'])){
				echo '<p class="description">' . $value['desc'] . '</p>';
			}

			echo '</div></section>';
			echo '<script type="text/javascript">
			jQuery(".chzn-select-deselect-'.$value['id'].'").chosen();
			</script>';
		}

		/* Display slide*/
		function slider($value)
		{
		    if (isset($this->saved_options[$value['id']])) {
				$current = $this->saved_options[$value['id']];
			} else {
				$current = $value['std'];
			}
			echo '<section><label>' . $value['name'] . '</label><div>';
			echo '<div class="slider slider_'.$value['id'].'"></div> 
					<span id="slider-result slider-result'.$value['id'].'" >50</span>   
                    <span>PX</span>
					<input type="hidden" name="'.$value['id'].'" id="'.$value['id'].'" value="';
			
			if (isset($this->saved_options[$value['id']])) {
				echo stripslashes($this->saved_options[$value['id']]);
			} else {
				echo $value['std'];
			}
			
			echo '" />';
			echo '</div></section>';
			echo '<script type="text/javascript">
	             jQuery( ".slider_'.$value['id'].'" ).slider({
			    animate: true,
                range: "min",
                value: "60",
                min: "'.$value['min'].'",
                max: "'.$value['max'].'",
				step: "'.$value['step'].'",
                
				//this gets a live reading of the value and prints it on the page
                slide: function( event, ui ) {
                    jQuery( "#slider-result'.$value['id'] .'").html( ui.value );
					jQuery( "#'.$value['id'] .'").html( ui.value );
					
                },

				//this updates the hidden form field so we can submit the data using a form
                change: function(event, ui) { 
                jQuery("#hidden").attr("value", ui.value);
                }
				});
				  </script>';
		}
		
		/* displays a radio*/
		function radio($value) {
			
			if (isset($this->saved_options[$value['id']])) {
				$checked_key = $this->saved_options[$value['id']];
			} else {
				$checked_key = $value['std'];
			}
			echo '<section><label>' . $value['name'] . '</label><div>';
			$i = 0;
			foreach($value['options'] as $key => $option) {
				$i++;
				$checked = '';
				if ($key == $checked_key) {
					$checked = ' checked="checked"';
				}
				
				echo '<input type="radio" id="' . $value['id'] . '_' . $i . '" name="' . $value['id'] . '" value="' . $key . '" ' . $checked . ' />';
				echo '<label for="' . $value['id'] . '_' . $i . '" class="radio">' . $option . '</label>';
				echo '<script>jQuery(function(){
			jQuery("#'.$value["id"]. '_' . $i.'").uniform();
			});</script>';
			}

			if(isset($value['desc'])){
				echo '<p class="description">' . $value['desc'] . '</p>';
			}
			
			echo '<div></section>';
			
		}
		

		/* displays a checkbox*/
		function checkbox($value) {
			$checked = '';
			if (isset($this->saved_options[$value['id']])) {
				if ($this->saved_options[$value['id']] == true) {
					$checked = 'checked="checked"';
				}
			} elseif ($value['std'] == true) {
				$checked = 'checked="checked"';
			}
			
			echo '<section><label>' . $value['name'] . '</label><div>';
			echo '<input type="checkbox" name="' . $value['id'] . '" id="' . $value['id'] . '" value="true" ' . $checked . ' />';
			if(isset($value['desc'])){
				echo '<span class="checkbox-description">' . $value['desc'] . '</span>';
			}
			echo '</div></section>';
			echo '
			<script>jQuery(function(){
			jQuery("#'.$value["id"].'").uniform();
			});</script>';
		}



		/* select target*/
		function get_select_target_options($type) {
			$options = array();
			switch($type){
				case 'page':
					$entries = get_pages('title_li=&orderby=name');
					foreach($entries as $key => $entry) {
						$options[$entry->ID] = $entry->post_title;
					}
					break;
				case 'category':
					$entries = get_categories('orderby=name&hide_empty=0');
					foreach($entries as $key => $entry) {
						$options[$entry->term_id] = $entry->name;
					}
					break;
				case 'post':
					$entries = get_posts('orderby=title&numberposts=-1&order=ASC');
					foreach($entries as $key => $entry) {
						$options[$entry->ID] = $entry->post_title;
					}
					break;
			}
			
			return $options;
		}


	}//END CLASS

}


?>